<template>
	<view class="control">
		<view class="" style="width: 100%;height: 20rpx;"></view>
		<view class="section-1">
			<view class="image">
				<image :src="data.cover"></image>
				<view class="" style="position: absolute;left: 0;top: 0;font-size: 24rpx;z-index: 99;border-radius: 12rpx;background-color: #ffaa00;color: #FFFFFF;padding: 5rpx 10rpx;"  v-if="data.scheme_support == 1">
					团购
				</view>
				<view class="num"> {{data.sold_num}}人已报名 </view>
			</view>
			<view class="detail">
				<view class="title">{{ data.name }}</view>
				<view class="tag-list flex-align">
					<view class="tag" v-for="(item, index) in spec" :key="index">{{item.name}}</view>
				</view>
				<view style="display: flex;flex-direction: row;	margin-top: 10rpx;height: 40rpx;" >
					<view style="width: 326rpx;font-size: 22rpx;color: #A6A6A6;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;" v-if="data.scheme_support == 1">{{data.scheme_name}}</view>
				</view>
				<view class="price-1">￥{{ price }}</view>
			</view>
		</view>
		<!-- 满{{data.scheme_cnt}}人优惠{{data.scheme_discount_amount}}元 -->
		<view class="" style="border-radius: 10rpx;background-color: #ffffff;margin-top: 20rpx;width: 710rpx;margin-left: 20rpx;"  v-if="data.scheme_support == 1">
			<u-notice-bar :text="data.scheme_desc" direction="row" color="#000000"
				fontSize='14' bgColor="none"></u-notice-bar>
		</view>
		<view class="section-2">
			<!-- <view class="detail flex-between">
				<view class="flex-align">
					<view class="price-1">￥{{ price }}</view>
					<view class="price-2">{{ data.classNum }}节课</view>
				</view>
				<view class="price-3">剩余{{ chooseStock }}名额</view>
			</view> -->

			<view class="list-title" style=" display: flex;align-items: center;justify-content: space-between;" v-if="data.train_enum_id == 3"
				@click="addStudent">
				需选择({{data.group_member_num}}名)以上报名人员(已选{{ selectLength }})
				<!-- 剩余可选人员({{ choosenum }}) -->
				<!-- <view style="width: 32rpx;height: 32rpx;color: #4BA677;font-size: 66rpx;">
					+
				</view> -->
				<image style="width: 40rpx;height: 40rpx;"
					src="https://oss.szsportscenter.cn/img/91e66c367cf85fedebbbf3834c7aa4a97129ad57.png" mode=""></image>
				<!-- <u-icon name="plus" color="#4BA677" label="添加"></u-icon> -->
			</view>
			<view class="list-title" style=" display: flex;align-items: center;justify-content: space-between;" v-else
				@click="addStudent">
				需选择({{choosenum}}名)以上报名人员(已选{{ selectLength }})
				<!-- 剩余可选人员({{ choosenum }}) -->
				<!-- <view style="width: 32rpx;height: 32rpx;color: #4BA677;font-size: 66rpx;">
					+
				</view> -->
				<image style="width: 40rpx;height: 40rpx;"
					src="https://oss.szsportscenter.cn/img/91e66c367cf85fedebbbf3834c7aa4a97129ad57.png" mode=""></image>
				<!-- <u-icon name="plus" color="#4BA677" label="添加"></u-icon> -->
			</view>
			<!-- <view style="
			  display: flex;
			  align-items: center;
			  justify-content: space-between; margin-top: 30rpx;">
			</view> -->

			<view class="kong flex-align" v-if="studentList.length < 1">
				<image mode="aspectFit"
					src="https://oss.szsportscenter.cn/img/db48bf10cb8573d3c80085c76e4afadd71783fcd.png">
				</image>
				<view>
					<view>报名学员资料为空</view>
					<view>请登记学员信息</view>
				</view>
			</view>
			<view class="list" v-if="studentList.length > 0">
			<!-- :style="{ height: app_height * 0.7 + 'rpx' }" -->
				<scroll-view class="scroll-view_H" scroll-y="true" >
					<checkbox-group shape="circle">
						<label class="item flex-between" v-for="(item, index) in studentList" :key="index"
							@click="radioChecked(index, item)">
							<view class="flex-align">
								<u--image width="50" height="50" shape="circle" :src="item.head_img">
								</u--image>
								<view class="info">
									<view class="name">{{ item.name }}</view>
									<view class="phone">{{ item.mobile }}</view>
								</view>
							</view>
							<view class="radioBox" style="padding-left: 34rpx">
								<checkbox color="#4BA677" :value="item.id" :checked="item.checked"
									:disabled="item.disabled" />
							</view>

						</label>
					</checkbox-group>
				</scroll-view>
			</view>
		</view>

		<!-- <view class="section-3 flex-center">
      <u-icon
        color="#4BA677"
        size="28"
        name="plus-circle-fill"
        @click="openAdd"
      ></u-icon>
      <text>添加报名人员</text>
    </view> -->
		<!-- train_enum_id == 1  短训培训班-->
		<!-- train_enum_id == 2 长训-->
		<!-- train_enum_id == 3 小班-->
		<view class="footer flex-align" v-if="data.train_enum_id == 1">
			<view class="btn" @click="doPay" v-if="selectLength >= choosenum">开始报名<text  v-if="data.scheme_support == 1 && selecttype == 1">(团购价{{amount (selectnum) }})</text> </view>
			<view class=" btn-disbled" v-else>开始报名</view>
		</view>
		<view class="footer flex-align" v-if="data.train_enum_id == 2">
			<view class="btn" @click="doPay" v-if="selectLength >= choosenum">开始报名</view>
			<view class=" btn-disbled" v-else>开始报名</view>
		</view>
		<view class="footer flex-align" v-if="data.train_enum_id == 3">
			<view class="btn" @click="doPay" v-if="selectLength === data.group_member_num">开始报名</view>
			<view class="btn-disbled" v-else>开始报名</view>
		</view>
		<view v-if="Write_off"
			style="width: 100%; height: 100%;background-color:rgba(0, 0, 0, 0.6);  position: absolute;top:0;left:0;z-index: 997;">
		</view>
		<view v-if="Write_off" 
			style="width: 76%; height: 740rpx;background-image: url('https://oss.szsportscenter.cn/upload/59/2ad663afb0de8b4acf48c4ee7ebe54.png?attname=160436.png');
			background-size: 100% 100%;position: absolute;top:350rpx;left:12%;z-index: 999;">
			<scroll-view scroll-y style="width: 100%;height: 400rpx;padding: 35rpx 45rpx 35rpx 45rpx;margin-top: 220rpx;" >
				<u-parse :content="user_instruction"></u-parse>
				<view class="" style="line-height: 50rpx;color: #ffaa00;font-weight: 600;font-size: 30rpx;">
					尊敬的会员用户
				</view>
				<view class="" style="line-height: 60rpx;margin-top: 10rpx;font-size: 30rpx;">
					{{dialog_text}}
				</view>
			</scroll-view>
			<view class="" style="display: flex;justify-content: center;align-items: center;">
				<view @click="offWrite" style="width: 180rpx; height: 60rpx;background: linear-gradient(270deg, #47B37B 0%, #69D19B 100%, #4BA677 0%);border-radius: 30rpx;
						background-size: 100% 100%;text-align: center;line-height: 60rpx;color: #fff;">
					确定
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const train = require("@/api/train/index.js");
	const index = require("@/api/index/index.js");
	export default {
		data() {
			return {
				id: 0,
				data: {},
				price: "0.00",
				studentList: [],
				venue_name: "",
				venue_id: 0,
				checkedNum: 0,
				spec: [],
				chooseStock: 0,
				chooseStudents: [],
				students: [],
				app_height: 0,
				arrid: [],
				selectLength: 0,
				quantity: 0,
				choosenum: '',
				skucode: "",
				sku: '',
				applicantlist: [],
				applicant: '',
				train_enum_id: '',
				sku_id: '',
				Write_off:false,
				dialog_text:'',
				selectnum:0,
				selectnums:0,
				selecttype:0
			};
		},

		onLoad(options) {
			let _this = this;
			uni.getSystemInfo({
				success: (res) => {
					console.log("手机可用高度:" + res.windowHeight * 2 + "rpx");
					_this.app_height = res.windowHeight * 2;
				},
			});
			this.train_enum_id = options.train_enum_id
			if (this.train_enum_id == 1) {
				this.id = options.id;
				this.price = options.price;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.chooseStock = options.chooseStock;
				this.spec = JSON.parse(options.checkedSpec);
				this.sku = options.sku
				this.skucode = options.skucode
				this.sku_id = options.sku_id
			} else if (this.train_enum_id == 2) {
				this.id = options.id;
				this.chooseStock = options.stock;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.price = options.price_selling;
				this.sku_id = options.sku_id
				this.sku = options.sku
				this.skucode = options.skucode
			} else if (this.train_enum_id == 3) {
				// 小班
				this.id = options.id;
				this.chooseStock = options.less_num;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.price = options.price_selling;
				this.sku_id = options.sku_id
				this.sku = options.sku
				this.skucode = options.skucode
			}
			this.getTrainDetail();
			// 获取核销领券弹窗
			// this.getParamsConfig()
		},
		onShow() {
			this.studentList = []
			this.students = [];
			this.checkedNum = 0;
			this.applicant = 0
			this.selectLength = 0
			this.applicantlist = []
			this.selectStudent();
		},
		onUnload() {
			this.studentList = []
			this.students = [];
			this.checkedNum = 0;
			this.applicant = 0
			this.selectLength = 0
			this.applicantlist = []
		},
		onHide() {
			let that = this;
			that.applicant = 0
			this.applicantlist = []
		},
		methods: {
			amount(value) {
				return parseFloat(value).toFixed(2);
			},
			// 获取核销领券弹窗
			async getParamsConfig() {
				let that = this;
				let postData = {}
				let res = await index.getParamsConfig(postData)
				if (res.code == 1) {
					if (res.data.show_dialog == 1) {
						this.Write_off = true
						this.dialog_text = res.data.dialog_text_train
					}
				}
			},
			//关闭弹窗
			offWrite() {
				this.Write_off = false
			},
			radioChecked(index, item) {
				// <!-- train_enum_id == 1  短训培训班-->
				// <!-- train_enum_id == 2 长训-->
				// <!-- train_enum_id == 3 小班-->
				if (this.train_enum_id != 3) {
					this.applicantlist = []
					item.checked = !item.checked
					this.selectLength = this.studentList.filter(item => item.checked).length || 0
					// let applicant = []
					this.studentList.forEach((el, idx) => {
						if (el.checked) {
							this.applicantlist.push(el.id)
						}

					})
					this.applicant = this.applicantlist.toString()
					// if (this.selectLength >= this.choosenum) {
					// 	this.studentList.forEach((el, idx) => {
					// 		if (idx == index) {
					// 			el.disabled = false

					// 		} else {
					// 			// el.disabled = true
					// 			if(el.checked){

					// 			} else {
					// 				el.disabled = true
					// 			}
					// 		}
					// 	})
					// } else {
					// 	this.studentList.forEach((el, idx) => {
					// 		el.disabled = false
					// 	})
					// }
					if(this.data.scheme_support == 1){
						if(this.selectLength == this.data.scheme_cnt){
							this.selectnum = (this.price * this.selectLength - this.data.scheme_discount_amount)
							this.selectnums = this.data.scheme_discount_amount
							this.selecttype = 1
						}else{
							this.selectnum = 0
							this.selectnums = 0
							this.selecttype = 0
						}
					}
					

				} else {
					this.applicantlist = []
					item.checked = !item.checked
					this.selectLength = this.studentList.filter(item => item.checked).length || 0
					this.studentList.forEach((el, idx) => {
						if (el.checked) {
							this.applicantlist.push(el.id)
						}
					})
					this.applicant = this.applicantlist.toString()
				}
			},

			async selectStudent() {
				let postData = {
					page: 1,
					pageSize: 10,
					openid: uni.getStorageSync('openid'),
					app_id: this.$https.weixinAppId,
					project_id: getApp().globalData.projectId,
				};
				let res = await train.selectStudent(postData);
				if (res.code == 1) {
					this.studentList = res.data.train;
				}
			},
			addStudent() {
				let field = JSON.stringify(this.data.field_ids);
				// uni.navigateTo({
				// 	url: "/pages_other/train/addInfo?field=" + field,
				// });
				uni.navigateTo({
					url: "/pages_other/train/addInfo?id=" + this.data.id,
				});
				// uni.navigateTo({
				// 	url: "/page_venues/pages/student-archives/add/add" ,
				// });
			},
			// openAdd() {
			//   let field = JSON.stringify(this.data.field_ids);
			//   uni.navigateTo({
			//     url: "/pages_other/train/addPreson?field=" + field,
			//   });
			// },
			// 获取培训班详情
			async getTrainDetail() {
				let postData = {
					train_id: this.id,
					app_id: this.$https.weixinAppId,
					project_id: getApp().globalData.projectId,
					sku: this.sku
				}
				let res = await train.getTrainDetail(postData);
				if (res.code == 1) {
					this.data = res.data.train;
					this.quantity = this.data.quantity
					this.choosenumlist()
				}
			},
			choosenumlist() {
				if (this.quantity >= this.chooseStock) {
					this.choosenum = this.chooseStock
				} else {
					this.choosenum = this.quantity
				}
			},
			doPay() {
				let ticketID = this.data.id;
				this.students = [];
				this.studentList.forEach((item) => {
					if (item.checked === true) {
						this.students.push(item);
					}
				});
				let checkedSpec = JSON.stringify(this.spec);
				uni.navigateTo({
					url: `/page_venues/pages/venuesDetail/ticketModule/makeOther_train?selectionArr=${encodeURIComponent(JSON.stringify(this.students))}` +
						"&ticketID=" +
						ticketID +
						"&pay_wait_time=" +
						this.data.pay_wait_time +
						"&windows=" +
						this.data.windows +
						"&venue_name=" +
						this.venue_name +
						"&skucode=" +
						this.skucode +
						"&sku_id=" +
						this.sku_id +
						"&sku=" +
						this.sku +
						"&venue_id=" +
						this.venue_id +
						"&applicant=" +
						this.applicant +
						"&goods_type=train" +
						"&spec=" +
						JSON.stringify(this.spec) +
						"&classNum=" +
						this.data.classNum +
						"&price=" +
						this.price +
						"&train_price=" +
						(this.price * this.selectLength - this.selectnums)+
						"&selectLength=" +
						this.selectLength +
						"&checkedSpec=" +
						checkedSpec +
						"&students=" +
						JSON.stringify(this.students),
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.control {
		width: 100%;
		min-height: 100vh;
		background-color: #EDF5F5;

		.section-1 {
			width: 710rpx;
			height: 300rpx;
			padding: 20rpx 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			margin-left: 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			border-radius: 10rpx;

			.image {
				display: flex;
				flex-direction: column;
				position: relative;

				image {
					width: 220rpx;
					height: 240rpx;
				}

				.num {
					width: 100%;
					text-align: center;
					background-color: #000000;
					opacity: 0.4;
					padding: 10rpx 0;
					font-size: 24rpx;
					color: #fff;
					position: absolute;
					bottom: 0;
				}
			}

			.detail {
				margin-left: 20rpx;

				.title {
					font-size: 28rpx;
					font-weight: bold;
					color: #000000;
					margin-top: 20rpx;
				}

				.price-1 {
					color: #4BA677;
					font-size: 36rpx;
					font-weight: 700;
					margin-right: 12rpx;
					margin-top: 20rpx;
				}

				.tag-list {
					margin-top: 20rpx;

					.tag {
						font-size: 22rpx;
						border-radius: 6rpx;
						padding: 5rpx 10rpx;
						color: #4BA677;
						margin-right: 10rpx;
						border: 1rpx solid #4BA677;

					}
				}
			}
		}

		.section-2 {
			border-radius: 10rpx;
			background-color: #ffffff;
			padding: 20rpx 30rpx 20 30rpx;
			margin-top: 20rpx;
			width: 710rpx;
			margin-left: 20rpx;
			height: 62vh;

			.detail {
				// margin-left: 280rpx;

				.price-1 {
					color: #ff4105;
					font-size: 32rpx;
					font-weight: 700;
					margin-right: 12rpx;
				}

				.price-2 {
					color: orange;
					font-size: 20rpx;
				}

				.price-3 {
					color: orange;
					font-size: 20rpx;
				}
			}

			.list-title {
				font-size: 28rpx;
				font-weight: bold;
				color: #000000;
				margin-top: 20rpx;
				padding: 10rpx 20rpx;
			}

			.list {
				.scroll-view_H {
					height: 57vh;
				}

				.item {
					margin-top: 30rpx;
					padding: 0 20rpx;

					.info {
						margin-left: 20rpx;

						.name {
							font-size: 26rpx;
							font-weight: 500;
							color: #333333;
						}

						.phone {
							font-size: 24rpx;
							font-weight: 500;
							color: #bebebe;
							margin-top: 10rpx;
						}
					}
				}
			}
		}

		.section-3 {
			flex-direction: column;

			text {
				margin-top: 15rpx;
				font-size: 22rpx;
				font-weight: 500;
				color: #333333;
			}
		}

		.kong {
			width: 100%;
			margin-top: 40rpx;
			justify-content: center;
			flex-direction: column;
			font-size: 26rpx;
			color: #999999;

			image {
				width: 420rpx;
				height: 300rpx;
			}
		}

		.footer {
			position: fixed;
			width: 100%;
			background-color: white;
			bottom: 0;
			left: 0;
			padding: 20rpx 25rpx 60rpx 25rpx;
			box-sizing: border-box;

			.menu {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 40rpx;

				image {
					width: 35rpx;
					height: 35rpx;
				}

				text {
					color: rgb(51, 51, 51);
					font-size: 24rpx;
					font-weight: 700;
					margin-top: 13rpx;
				}
			}

			.btn {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background: linear-gradient(270deg, #47B37B 0%, #69D19B 100%, #4BA677 0%);
				text-align: center;
				font-weight: 700;
			}

			.btn-disbled {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #bebebe;
				text-align: center;
				font-weight: 700;
			}
		}

		.checkbox .wx-checkbox-input {
			border-radius: 26upx;
			/* 圆角 */
			width: 26upx;
			/* 背景的宽 */
			height: 26upx;
			/* 背景的高 */
			border: 1px solid #ccc;
			background: #eee;
		}

		/* 复选框禁用样式 */

		.checkbox .wx-checkbox-input-disabled {
			border-radius: 26upx;
			/* 圆角 */
			width: 26upx;
			/* 背景的宽 */
			height: 26upx;
			/* 背景的高 */
			background: #F2F7F5;
			border: 1px solid rgba(201, 201, 201, 1);
		}

		/* 复选框选中后样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			background: rgba(255, 129, 92, 1);
			border: 1px solid rgba(255, 129, 92, 1);
		}

		/*checkbox选中后图标样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
			width: 26upx;
			height: 26upx;
			line-height: 26upx;
			text-align: center;
			font-size: 24upx;
			color: #fff;
			background: transparent;
			transform: translate(-50%, -50%) scale(1);
			-webkit-transform: translate(-50%, -50%) scale(1);
		}

		/deep/ checkbox .wx-checkbox-input {
			border-radius: 50% !important;
			color: #ffffff !important;
		}

		/deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			color: #fff;
			background: linear-gradient(270deg, #47B37B 0%, #69D19B 100%), #4BA677;
		}

		/deep/.wx-checkbox-input.wx-checkbox-input-checked {
			border: none !important;
		}

	}
</style>